clear
clear matrix
clear mata
program drop _all
set more off
set emptycells drop
set maxvar 100000
tempfile main
cap log close
log using AppendixF,replace

cd "<directory>"

*The code in this .do file reads in the dataset created in Data Cleaning Step 2.do and runs the regressions for Appendix F;
*Before the estimation commands are run, the quarter of the rule change is dropped;
*The "_m" suffixes in the outreg commands stipulate which DPO window the model corresponds to:;
*	_m1 = DPO[-4,-7];
*	_m2 = DPO[-5,-8];
*	_m3 = DPO[-6,-9];
*	_m4 = DPO[-7,-10];

/*********************************************************************************
*Note: The following block of code appears before every regression. This code identifies and drops observations that Stata will automatically drop in the course of the probit estimation. This code is optional and all the regressions can be run without it. However, using this code speeds up the estimation significantly, because Stata does not have to determine whether to drop these obervations as it goes through the estimation;
*	tempfile msayq
*	keep if !missing(transfer, post, dspp0_5t8, post_dspp0_5t8, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, origyq, ssmsa, distyq)
*	keep if dcountss_2==1
*	keep if yearmonth>=572 & yearmonth<=620
*	collapse (mean) transfer,by(msayq)
*	gen indic=1 if transfer==0
*	keep msayq indic
*	sa `msayq',replace
*	u `main',clear
*	merge m:1 msayq using `msayq'
*	drop if _merge==2
*	drop _merge
***********************************************************************************/


u final_dataset,clear
drop if distyq==198
egen msapost=group(msa post)
sa `main',replace




u `main',clear
tempfile ssmsa
keep if !missing(transfer, post, dspp0_5t8, post_dspp0_5t8, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, origyq, ssmsa, distyq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_5t8 post_dspp0_5t8,by(ssmsa)
gen indic=1 if transfer==0
keep ssmsa indic
sa `ssmsa',replace
u `main',clear
merge m:1 ssmsa using `ssmsa'
drop if _merge==2
drop _merge
 

probit transfer dspp0_5t8 post_dspp0_5t8 post i.distyq orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.dealtypeid i.originatorid i.ssmsa i.origyq if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using AppendixF_m2, auto(2) excel replace stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, Y, PropType FE, Y, DealType FE, Y, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, SE Clust by loan, Y, Estimation, Probit)





u `main',clear
tempfile ssmsa
keep if !missing(transfer, post, dspp0_4t7, post_dspp0_4t7, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, origyq, ssmsa, distyq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_4t7 post_dspp0_4t7,by(ssmsa)
gen indic=1 if transfer==0
keep ssmsa indic
sa `ssmsa',replace
u `main',clear
merge m:1 ssmsa using `ssmsa'
drop if _merge==2
drop _merge


probit transfer dspp0_4t7 post_dspp0_4t7 post i.distyq orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.dealtypeid i.originatorid i.ssmsa i.origyq if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using AppendixF_m1, auto(2) excel replace stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, Y, PropType FE, Y, DealType FE, Y, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, SE Clust by loan, Y, Estimation, Probit)





u `main',clear
tempfile ssmsa
keep if !missing(transfer, post, dspp0_6t9, post_dspp0_6t9, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, origyq, ssmsa, distyq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_6t9 post_dspp0_6t9,by(ssmsa)
gen indic=1 if transfer==0
keep ssmsa indic
sa `ssmsa',replace
u `main',clear
merge m:1 ssmsa using `ssmsa'
drop if _merge==2
drop _merge


 
probit transfer dspp0_6t9 post_dspp0_6t9 post i.distyq orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.dealtypeid i.originatorid i.ssmsa i.origyq if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using AppendixF_m3, auto(2) excel replace stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, Y, PropType FE, Y, DealType FE, Y, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, SE Clust by loan, Y, Estimation, Probit)





u `main',clear
tempfile ssmsa
keep if !missing(transfer, post, dspp0_7t10, post_dspp0_7t10, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, origyq, ssmsa, distyq)
keep if dcountss_2==1
keep if yearmonth>=572 & yearmonth<=620
collapse (mean) transfer post dspp0_7t10 post_dspp0_7t10,by(ssmsa)
gen indic=1 if transfer==0
keep ssmsa indic
sa `ssmsa',replace
u `main',clear
merge m:1 ssmsa using `ssmsa'
drop if _merge==2
drop _merge


probit transfer dspp0_7t10 post_dspp0_7t10 post i.distyq orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi i.propid i.dealtypeid i.originatorid i.ssmsa i.origyq if yearmonth>=572 & yearmonth<=620 & nouse2==0 & dcountss_2==1 & indic!=1,cluster(masterloanidtrepp)
outreg2 using AppendixF_m4, auto(2) excel replace stats(coef se) addstat(Pseudo R2, e(r2_p)) addtext(Orig Year-Q FE, Y, Curr Year-Q FE, N, SS FE, Y, PropType FE, Y, DealType FE, Y, Originator FE, Y, MSA-Year-Q FE, Y, MSA-SS FE, N, SE Clust by loan, Y, Estimation, Probit)